package me.zhengjie.org.teamemployee.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.org.teamemployee.domain.TeamEmployee;
import me.zhengjie.org.teamemployee.service.TeamEmployeeService;
import me.zhengjie.org.teamemployee.service.dto.TeamEmployeeQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author ZGZ
* @date 2020-04-14
*/
@Api(tags = "组织人员管理")
@RestController
@RequestMapping("/api/teamEmployee")
public class TeamEmployeeController {

    private final TeamEmployeeService teamEmployeeService;

    public TeamEmployeeController(TeamEmployeeService teamEmployeeService) {
        this.teamEmployeeService = teamEmployeeService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('teamEmployee:list')")
    public void download(HttpServletResponse response, TeamEmployeeQueryCriteria criteria) throws IOException {
        teamEmployeeService.download(teamEmployeeService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询组织人员")
    @ApiOperation("查询组织人员")
    @PreAuthorize("@el.check('teamEmployee:list')")
    public ResponseEntity<Object> getTeamEmployees(TeamEmployeeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(teamEmployeeService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增组织人员")
    @ApiOperation("新增组织人员")
    @PreAuthorize("@el.check('teamEmployee:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody TeamEmployee resources){
        return new ResponseEntity<>(teamEmployeeService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改组织人员")
    @ApiOperation("修改组织人员")
    @PreAuthorize("@el.check('teamEmployee:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody TeamEmployee resources){
        teamEmployeeService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除组织人员")
    @ApiOperation("删除组织人员")
    @PreAuthorize("@el.check('teamEmployee:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        teamEmployeeService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}